package com.ruoyi.room.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.room.domain.Room;
import com.ruoyi.room.domain.RoomGood;
import com.ruoyi.room.mapper.RoomMapper;
import com.ruoyi.room.service.IRoomGoodService;
import com.ruoyi.room.service.IRoomRecordService;
import com.ruoyi.room.service.IRoomService;
import com.ruoyi.room.util.MD5Utils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房间信息Controller
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/room/room")
public class RoomController extends BaseController {
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IRoomGoodService roomGoodService;
    @Autowired
    private IRoomRecordService roomRecordService;
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询房间信息列表
     */
    @PreAuthorize("@ss.hasPermi('room:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(Room room) {
        startPage();
        room.setIsDelete(0);
        List<Room> list = roomService.selectRoomList(room);
        return getDataTable(list);
    }

    /**
     * 导出房间信息列表
     */
    @PreAuthorize("@ss.hasPermi('room:room:export')")
    @Log(title = "房间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Room room) {
        List<Room> list = roomService.selectRoomList(room);
        ExcelUtil<Room> util = new ExcelUtil<Room>(Room.class);
        util.exportExcel(response, list, "房间信息数据");
    }

    /**
     * 获取房间信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('room:room:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Room room = roomService.selectRoomById(id);
        RoomGood param = new RoomGood();
        param.setRoomId(id);
        List<RoomGood> roomGoods = roomGoodService.selectRoomGoodList(param);
        room.setRoomGoods(roomGoods);
        return success(room);
    }

    /**
     * 新增房间信息
     */
    @PreAuthorize("@ss.hasPermi('room:room:add')")
    @Log(title = "房间信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Room room) {
        Long type = room.getType();
        if (type == 2) {
            String passwordCode = room.getPasswordCode();
            if (StringUtils.isNotEmpty(passwordCode)) {
                room.setPasswordCode(passwordCode);
                room.setMd5PasswordCode(MD5Utils.md5Hex(passwordCode));
            }
        }
        Integer roomId = roomService.insertRoom(room);
        List<RoomGood> roomGoods = room.getRoomGoods();
        for (RoomGood roomGood : roomGoods) {
            roomGood.setRoomId(room.getId());
            roomGoodService.insertRoomGood(roomGood);
        }
        return toAjax(roomId);
    }

    /**
     * 修改房间信息
     */
    @PreAuthorize("@ss.hasPermi('room:room:edit')")
    @Log(title = "房间信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Room room) {
        Long type = room.getType();
        if (type == 2) {
            String passwordCode = room.getPasswordCode();
            if (StringUtils.isNotEmpty(passwordCode)) {
                room.setPasswordCode(passwordCode);
                room.setMd5PasswordCode(MD5Utils.md5Hex(passwordCode));
            }
        }
        RoomGood param = new RoomGood();
        param.setRoomId(room.getId());
        List<RoomGood> exist = roomGoodService.selectRoomGoodList(param);
        if (exist.size() > 0) {
            exist.forEach(e -> {
                roomGoodService.deleteRoomGoodById(e.getId());
            });
        }
        List<RoomGood> roomGoods = room.getRoomGoods();
        for (RoomGood roomGood : roomGoods) {
            roomGoodService.insertRoomGood(roomGood);
        }
        return toAjax(roomService.updateRoom(room));
    }

    /**
     * 删除房间信息
     */
    @PreAuthorize("@ss.hasPermi('room:room:remove')")
    @Log(title = "房间信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            Room room = new Room();
            room.setId(id);
            room.setIsDelete(1);
            roomService.updateRoom(room);
        }
        return toAjax(1);
    }
}
